﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using STT.Layer.Connection;
using System.Data;
using STT.Business;
using STT.Entities;
namespace STT.Profilehv.UserControls
{
    public partial class Index : System.Web.UI.UserControl
    {
        ToolBox tb = new ToolBox();
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadInfoHV();
        }
        private void LoadInfoHV()
        {
            DtlIndex.DataSource = tb.GetDataTableById("Get_HocVien_ByMaHV", Session["Mahv"].ToString());
            DtlIndex.DataBind();
        }
        protected void DtlIndex_CancelCommand(object source, DataListCommandEventArgs e)
        {

            DtlIndex.EditItemIndex = -1;
            LoadInfoHV();
         
        }

        protected void DtlIndex_DeleteCommand(object source, DataListCommandEventArgs e)
        {

        }

        protected void DtlIndex_EditCommand(object source, DataListCommandEventArgs e)
        {
            DtlIndex.EditItemIndex = e.Item.ItemIndex;
            LoadInfoHV();
        }

        protected void DtlIndex_UpdateCommand(object source, DataListCommandEventArgs e)
        {
            try
            {
                if (e.CommandName.Equals("Update"))
                {
                    ETbHocVien hv = new ETbHocVien();
                    hv.MaHV = DtlIndex.DataKeys[e.Item.ItemIndex].ToString();
                    TextBox txttuoi = (TextBox)e.Item.FindControl("txttuoi");
                    TextBox txttenhv = (TextBox)e.Item.FindControl("txttenhv");
                    TextBox txtquequan = (TextBox)e.Item.FindControl("txtquequan");
                    TextBox txtdiachi = (TextBox)e.Item.FindControl("txtdiachi");
                    TextBox txtsdt = (TextBox)e.Item.FindControl("txtsdt");
                    TextBox txtghichu = (TextBox)e.Item.FindControl("txtghichu");
                    TextBox txtanh = (TextBox)e.Item.FindControl("txtanh");
                    FileUpload fileup = (FileUpload)e.Item.FindControl("Fulanh");
                    if (fileup.HasFile)
                    {
                        if (txtanh.Text == "NotImage")
                        {
                            System.IO.File.Delete(Server.MapPath("~/Profilehv/img/gallery/") + txtanh.Text);
                            fileup.SaveAs(Server.MapPath("~/Profilehv/img/gallery/") + fileup.FileName);
                            hv.Anh = fileup.PostedFile.FileName.ToString();
                            hv.Tuoi = Convert.ToInt32(txttuoi.Text);
                            hv.TenHV = txttenhv.Text;
                            hv.QueQuan = txtquequan.Text;
                            hv.DiaChi = txtdiachi.Text;
                            hv.Sdt = txtsdt.Text;
                            hv.GhiChu = txtghichu.Text;
                            Business.BTbHocVien.UpdateInfoHV(hv);
                        }
                        else
                        {
                            fileup.SaveAs(Server.MapPath("~/Profilehv/img/gallery/") + fileup.FileName);
                            hv.Anh = fileup.PostedFile.FileName.ToString();
                            hv.Tuoi = Convert.ToInt32(txttuoi.Text);
                            hv.TenHV = txttenhv.Text;
                            hv.QueQuan = txtquequan.Text;
                            hv.DiaChi = txtdiachi.Text;
                            hv.Sdt = txtsdt.Text;
                            hv.GhiChu = txtghichu.Text;
                            Business.BTbHocVien.UpdateInfoHV(hv);
                        }
                    }
                    else
                    {
                        hv.Tuoi = Convert.ToInt32(txttuoi.Text);
                        hv.TenHV = txttenhv.Text;
                        hv.QueQuan = txtquequan.Text;
                        hv.DiaChi = txtdiachi.Text;
                        hv.Sdt = txtsdt.Text;
                        hv.GhiChu = txtghichu.Text;
                        hv.Anh = txtanh.Text;
                        Business.BTbHocVien.UpdateInfoHV(hv);
                    }
                    DtlIndex.EditItemIndex = -1;
                    Response.Redirect("Default.aspx?mod=TTCN117");
                }
            }
            catch { }
        }
    }
}